<template>
  <div class="home">
    <el-row :gutter="20" class="home-top">
      <el-col :span="24">
        <el-row :gutter="5" class="mgb20">
          <el-col :span="12">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-1">
                <i class="el-icon-sunrise grid-con-icon"></i>
                <div class="grid-cont-right">
                  <div class="grid-num">
                    <count-to
                      :start-val="0"
                      :end-val="echartData.today_total"
                      :duration="3600"
                    />
                  </div>
                  <div>二维码 今日访问量</div>
                </div>
              </div>
            </el-card>
          </el-col>
          <el-col :span="12">
            <el-card shadow="hover" :body-style="{ padding: '0px' }">
              <div class="grid-content grid-con-2">
                <i class="el-icon-pie-chart grid-con-icon"></i>
                <div class="grid-cont-right">
                  <div class="grid-num">
                    <count-to
                      :start-val="0"
                      :end-val="echartData.total"
                      :duration="3600"
                    />
                  </div>
                  <div>二维码 访问总数量</div>
                </div>
              </div>
            </el-card>
          </el-col>
        </el-row>
      </el-col>
    </el-row>
    <el-card shadow="hover" class="total">
      <div class="year"></div>
      <div class="mouth"></div>
    </el-card>
  </div>
</template>

<script>
import ECharts from "echarts";
import CountTo from "vue-count-to";
export default {
  components: {
    CountTo,
  },
  mounted() {
    this.getTotal();
  },
  data() {
    return {
      echartData: {},
    };
  },
  methods: {
    //获取统计数据
    async getTotal() {
      const { code, data, msg } = await this.$api.total.getTotal();
      if (!code) {
        this.echartData = data;
        this.initYearEchart(data.year_months);
        this.initMouthEchart(data.month_days);
        this.$message.success(msg);
      }
    },
    //   绘制年图表
    initYearEchart(year_months) {
      const seriesData = []; //月数据
      Object.keys(year_months.months).forEach((item) => {
        seriesData.push(year_months.months[item]);
      });
      const myChart = ECharts.init(document.querySelector(".year"));
      const option = {
        title: {
          text: `二维码 ${year_months.current_year} 访问统计`,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#2D8CF0",
            },
          },
        },
        legend: {
          data: ["邮件营销"],
        },
        toolbox: {
          feature: {
            //   saveAsImage: {},
          },
        },
        grid: {
          /*  left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true */
          width: "auto", //图例宽度
        },
        xAxis: [
          {
            type: "category",
            boundaryGap: false,
            data: [
              "一月",
              "二月",
              "三月",
              "四月",
              "五月",
              "六月",
              "七月",
              "八月",
              "九月",
              "十月",
              "十一月",
              "十二月",
            ],
            axisLabel: {
              interval: 0,
            },
          },
        ],
        yAxis: [
          {
            type: "value",
            splitLine: {
              show: false, //横向辅助线
            },
          },
        ],
        series: [
          {
            //   name: "邮件营销",
            type: "line",
            //   stack: "总量",
            areaStyle: {},
            /* emphasis: {
            focus: "series",
          }, */
            data: seriesData,
            areaStyle: {
              color: "#2D8CF0",
            },
            lineStyle: {
              color: "#2D8CF0",
            },
          },
        ],
      };

      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
    //绘制月柱状图
    initMouthEchart(month_days) {
      const seriesData = []; //天数据
      const xAxisData = []; //纵轴坐标
      Object.keys(month_days.days).forEach((item) => {
        xAxisData.push(item + "日");
        seriesData.push(month_days.days[item]);
      });

      const myChart = ECharts.init(document.querySelector(".mouth"));
      const option = {
        color: ["#64D572"],
        title: {
          text: `二维码 ${month_days.current_month} 访问统计`,
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            label: {
              backgroundColor: "#64D572",
            },
          },
        },
        xAxis: {
          type: "category",
          data: xAxisData,
          axisLabel: {
            interval: 1,
          },
        },
        yAxis: {
          type: "value",
          splitLine: {
            show: false,
          },
        },
        series: [
          {
            data: seriesData,
            type: "bar",
          },
        ],
      };
      // 使用刚指定的配置项和数据显示图表。
      myChart.setOption(option);
    },
  },
};
</script>


<style scoped lang='scss'>
/deep/.home {
  height: 100%;
  .home-top {
    // height: 20%;
  }
  .total {
    height: 80%;
    .el-card__body {
      height: 100%;
      display: flex;
      .year {
        flex: 1;
      }
      .mouth {
        flex: 1;
      }
    }
  }
}

.mgb20 {
  margin-bottom: 10px !important;
}

.grid-content {
  display: flex;
  align-items: center;
  height: 100px;
}

.grid-cont-right {
  flex: 1;
  text-align: center;
  font-size: 14px;
  color: #999;
}

.grid-num {
  font-size: 30px;
  font-weight: bold;
}

.grid-con-icon {
  font-size: 50px;
  width: 100px;
  height: 100px;
  text-align: center;
  line-height: 100px;
  color: #fff;
}

.grid-con-1 .grid-con-icon {
  background: rgb(45, 140, 240);
}

.grid-con-1 .grid-num {
  color: rgb(45, 140, 240);
}

.grid-con-2 .grid-con-icon {
  background: rgb(100, 213, 114);
}

.grid-con-2 .grid-num {
  color: rgb(45, 140, 240);
}

.grid-con-3 .grid-con-icon {
  background: rgb(242, 94, 67);
}

.grid-con-3 .grid-num {
  color: rgb(242, 94, 67);
}

.user-info {
  display: flex;
  align-items: center;
  padding-bottom: 20px;
  border-bottom: 2px solid #ccc;
  margin-bottom: 20px;
}

.user-avator {
  width: 120px;
  height: 120px;
  border-radius: 50%;
}

.user-info-cont {
  padding-left: 50px;
  flex: 1;
  font-size: 14px;
  color: #999;
}

.user-info-cont div:first-child {
  font-size: 30px;
  color: #222;
}

.user-info-list {
  font-size: 14px;
  color: #999;
  line-height: 25px;
}

.user-info-list span {
  margin-left: 70px;
}

.mgb20 {
  margin-bottom: 20px;
}

.todo-item {
  font-size: 14px;
}

.todo-item-del {
  text-decoration: line-through;
  color: #999;
}

.schart {
  width: 100%;
  height: 300px;
}
</style>
